package love.yuanmengrobot.zframe.mvc;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/7/3 17:34
 */


import love.yuanmengrobot.zframe.ioc.BeanFactoryTest;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Map;

public class ConfigurationContextListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public ConfigurationContextListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
        try {


//            System.out.println("开始1");
//            SqlSessionFactory sqlSessionFactory;
//    /*
//    相当于JDBC中的Connection
//     */
//              SqlSession sqlSession;
//
//                InputStream in = Class.forName("love.love.yuanmengrobot.zframe.mvc.ConfigurationContextListener").getResourceAsStream("/mybatis-config.xml");
//                sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//            sqlSession = sqlSessionFactory.openSession();
//            System.out.println("开始2");
////            System.out.println("ou000");
//            Class.forName("love.love.yuanmengrobot.zframe.ioc.BeanFactoryTest");


            Map<String,ControllerMapping> controllerMapping= new Configuration().config();
            /*
            服务端给网页（jsp）传数据，把数据放入某个作用域：request、session、application
             */
            sce.getServletContext().setAttribute("love.yuanmengrobot.web.controller",controllerMapping);


//            System.out.println("ou");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}
